package edu.bu.CS893.data;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import edu.bu.CS893.R;
import android.content.Context;

public class FacebookApiCredentialsReader {
	private static String TAG_API_KEY = "api_key";
	private static String TAG_SECRET_KEY = "secret_key";
	
	public static FacebookApiCredentials getCredentials(Context theContext) {
		XmlPullParser aPullParser = theContext.getResources().getXml(R.xml.facebook_api_credentials);

		String aApiKey = null;
		String aSecretKey = null;
		
		try {
			int aEventType = aPullParser.getEventType();
			String aCurrentTag = "";
			
			while (aEventType != XmlPullParser.END_DOCUMENT) {
				
				if (aEventType == XmlPullParser.START_TAG)
					aCurrentTag = aPullParser.getName();
				
				if (aEventType == XmlPullParser.TEXT) {
					if (aCurrentTag.equals(TAG_API_KEY))
						aApiKey = aPullParser.getText();
					else if (aCurrentTag.equals(TAG_SECRET_KEY))
						aSecretKey = aPullParser.getText();
				}
				
				if (aEventType == XmlPullParser.END_TAG)
					aCurrentTag = "";
				
				aEventType = aPullParser.next();
			}
			
			
		} 
		catch (XmlPullParserException e) { }
		catch (IOException e) { }
		
		return new FacebookApiCredentials(aApiKey, aSecretKey);
	}
}
